package com.android.lovingpet.util;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Environment;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.android.lovingpet.R;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.model.CropOptions;

import java.io.File;

/**
 * Created by ikata on 2018/5/14.
 */

public class MyPhotoWindow {
    private CropOptions cropOptions;
    private TakePhoto takePhoto;
    private PopupWindow popupWindow;
    private Activity activity;
    private View view;
    private Uri imageUri;
    //默认裁剪
    private boolean crop=true;

    public MyPhotoWindow(TakePhoto takePhoto, View view)
    {
        this.takePhoto=takePhoto;
        this.view= view;
        cropOptions = new CropOptions.Builder().setAspectX(1).setAspectY(1).setWithOwnCrop(false).create();
    }
    public boolean isCrop() {
        return crop;
    }

    public MyPhotoWindow setCrop(boolean crop) {
        this.crop = crop;
        return this;
    }

    public Uri getImageUri() {
        return imageUri;
    }
    public CropOptions getCropOptions() {
        return cropOptions;
    }

    public void setCropOptions(CropOptions cropOptions) {
        this.cropOptions = cropOptions;
    }


    public void create() {
        activity=(Activity)view.getContext();
        LinearLayout layout = (LinearLayout) activity.getLayoutInflater().inflate(R.layout.my_photo_window, null);
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }
        popupWindow = new PopupWindow(layout,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        //点击空白处时，隐藏掉pop窗口
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //添加弹出、弹入的动画
        popupWindow.setAnimationStyle(R.style.Popupwindow);
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.BOTTOM, 0, -location[1]);
        //添加按键事件监听
        setButtonListeners(layout);
    }
    public void setButtonListeners(LinearLayout layout) {
        Button camera =  layout.findViewById(R.id.camera);
        Button gallery = layout.findViewById(R.id.gallery);
        Button cancel =  layout.findViewById(R.id.cancel);
        camera.setOnClickListener(view -> {
            //相机
            if (popupWindow != null && popupWindow.isShowing()) {
                imageUri = getImageCropUri();
                if (crop==true) {
                    //拍照并裁剪
                    takePhoto.onPickFromCaptureWithCrop(imageUri, cropOptions);
                }
                else {
                    //仅仅拍照不裁剪
                    takePhoto.onPickFromCapture(imageUri);
                }
            }
            activity.overridePendingTransition(R.anim.window_back, R.anim.window_out);
            popupWindow.dismiss();

        });
        //相册
        gallery.setOnClickListener(view -> {
            if (Utility.hasSdcard() == true && popupWindow != null && popupWindow.isShowing()) {
                imageUri = getImageCropUri();
                if (isCrop()==true) {
                    //从相册中选取图片并裁剪
                    takePhoto.onPickFromGalleryWithCrop(imageUri, cropOptions);
                }
                else {
                    //从相册中选取不裁剪
                    takePhoto.onPickFromGallery();
                }
                popupWindow.dismiss();
            }
        });
        cancel.setOnClickListener(view -> {
            if (popupWindow != null && popupWindow.isShowing()) {
                popupWindow.dismiss();
            }
        });
    }
    //获得照片的输出保存Uri
    public Uri getImageCropUri() {
        File file=new File(Environment.getExternalStorageDirectory(), "/temp/"+System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists())file.getParentFile().mkdirs();
        return Uri.fromFile(file);
    }
}
